package main

import "fmt"

func main() {

	c := make(chan int)

	go func() {
		for i := 0; i < 5; i++ {
			c <- i
		}
		close(c) //注释掉会死锁 (all goroutines are asleep - deadlock)
	}()

	/* 次部分可以简写为 range*/
	/*for {
		if data, ok := <-c; ok {
			fmt.Println(data)
		} else {
			break
		}
	}*/

	for data := range c {
		fmt.Println(data)
	}
	fmt.Println("main end")
}
